﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace prjSeguranca
{
    public partial class frmMenuSeguranca : Form
    {
        public int opcao_sair;
        public Form f;
        public frmMenuSeguranca()
        {
            InitializeComponent();
        }

        public enum opcao
        {
            ComAnexo = 1,
            SemAnexo = 0
        }

        enum estado_form
        {
            abre = 0,
            fecha = 1
        }

        private void mostraForm(string titulo, FormGridPadrao.frmFormGridPadrao form, FormGridPadrao.frmFormGridPadrao form2,
            opcao opcao, estado_form estado, string tituloAnexo, EventHandler evento)
        {
            bool vExiste = false;
            int index = 0;
            int indice = 0;
            foreach (TabPage t in tbcntrlAbasGrid.TabPages)
            {
                if (titulo == t.Text)
                {
                    vExiste = true;
                    indice = index;
                }
                index++;
            }
            if ((estado == estado_form.abre) && (!vExiste))
            {
                pctrbxImagemFundo.Visible = false;
                TabPage tbpgNovaAba = new TabPage();
                tbpgNovaAba.Text = titulo;

                form.tbpgAba = tbpgNovaAba;

                Panel p1 = new Panel();
                p1.Dock = DockStyle.Fill;
                p1.BorderStyle = BorderStyle.None;

                form.TopLevel = false;
                form.Dock = DockStyle.Fill;

                form.modo_form = FormGridPadrao.frmFormGridPadrao.tipo.Principal;

                p1.Controls.Add(form);
                tbpgNovaAba.Controls.Add(p1);

                SplitContainer spltcntnrForms = new SplitContainer();
                spltcntnrForms.IsSplitterFixed = false;
                spltcntnrForms.BorderStyle = BorderStyle.None;
                form.spltcntnr = spltcntnrForms;
                spltcntnrForms.Dock = DockStyle.Fill;
                tbpgNovaAba.Controls.Add(spltcntnrForms);
                spltcntnrForms.Panel1.Controls.Add(p1);
                spltcntnrForms.Panel2Collapsed = true;

                tbcntrlAbasGrid.TabPages.Add(tbpgNovaAba);

                if (opcao == opcao.ComAnexo)
                {
                    form.addAnexo(tituloAnexo, null);
                    form2.TopLevel = false;
                    form2.AutoScroll = true;
                    form2.Dock = DockStyle.Fill;
                    form2.modo_form = FormGridPadrao.frmFormGridPadrao.tipo.Anexo;
                    form2.spltcntnr = spltcntnrForms;
                    TabControl tb1 = new TabControl();
                    tb1.Dock = DockStyle.Fill;
                    TabPage tbpgnovaaba2 = new TabPage();
                    tbpgnovaaba2.AutoScroll = true;
                    tbpgnovaaba2.Text = tituloAnexo;
                    tbpgnovaaba2.Controls.Add(form2);
                    tb1.TabPages.Add(tbpgnovaaba2);
                    spltcntnrForms.Panel2.Controls.Add(tb1);
                    form.f2 = form2;
                    form2.Show();
                    form2.carregaGrid();
                }

                //form.ShowIcon = false;
                tbcntrlAbasGrid.SelectedTab = tbpgNovaAba;
                form.tbcntrlAbasGrid = tbcntrlAbasGrid;
                form.carregaGrid();
                form.Show();
            }
            else if ((estado == estado_form.abre) && (vExiste))
            {
                tbcntrlAbasGrid.SelectTab(indice);
            }
        }

        private void tbcntrlAbasGrid_ControlRemoved_1(object sender, ControlEventArgs e)
        {
            if (tbcntrlAbasGrid.Controls.Count < 1)
            {
                pctrbxImagemFundo.Visible = true;
            }
        }

        private void btnUsuario_Click(object sender, EventArgs e)
        {
            frmGridUsuario frmGridUsuario = new frmGridUsuario();
            mostraForm("Usuários", frmGridUsuario, null, opcao.SemAnexo, estado_form.abre, null, null);
        }

        private void btnGrupoUsuario_Click(object sender, EventArgs e)
        {
            frmGridGrupoUsuario frmGridGrupoUsuario = new frmGridGrupoUsuario();
            mostraForm("Grupo de Usuário", frmGridGrupoUsuario, null, opcao.SemAnexo, estado_form.abre, null, null);
        }

        private void frmMenuSeguranca_FormClosing(object sender, FormClosingEventArgs e)
        {
            /*
            frmSair sair = new frmSair();
            sair.f = this;
            sair.ShowDialog();
            if (opcao_sair == 0)
            {
                f.Dispose();
            }
            else if (opcao_sair == 1)
            {
                f.Visible = true;
            }
            else
            {
                e.Cancel = true;
            }
             * */
        }
    }
}
